%% Figure 3: Impact of central clearing on expected default losses in a core-periphery network
clear all; clc

%% plot for different N
Calibration

Ns = (2:16)*3;
delta_DL_per = [];
delta_DL_core = [];
delta_TDL = [];
for i=1:length(Ns)
    N = Ns(i);

    % impact on peripheral entity
    delta_DL_per(i) = f(K-1,beta)/f(K,beta) + (1-pi^(2*N/3-1))/(1-pi) * xi(alpha_CCP)/xi(alpha_uc) * f(1,beta)/f(K,beta) - 1;

    % impact on core entity
    delta_DL_core(i) = f(K-1,beta)/f(K,beta) + pi^(2*N/3-1) * 6*G_per/((N-3)+6*G_per) * (1-pi^(N/3))/(1-pi) * xi(alpha_CCP)/xi(alpha_uc) * f(1,beta)/f(K,beta)-1;

    % impact on aggregate expected default loss
    eta_agg = 6*G_per/(12*G_per + N - 3);
    delta_TDL(i) = xi(alpha_CCP)/xi(alpha_uc)*f(1,beta)/f(K,beta)*eta_agg + f(K-1,beta)/f(K,beta)-1;
end


FontSize = 19;
linetypes = {'-','-',':','-.','--','--',':'};
linemarkers = {'none','o','none','.','s','d','+'};

h1 = figure();
hold on
p1=plot(Ns,delta_TDL,'-s','Color','black','LineWidth',1.2);
p2=plot(Ns,delta_DL_per,'--red','LineWidth',1.2);
p3=plot(Ns,delta_DL_core,'-blue','LineWidth',1.2);
grid on
xlabel('Number of market participants ($N$)','FontSize',10,'Interpreter','latex')
ylabel('$$\Delta$$Expected default loss ($$\Delta DL$$)','FontSize',10,'Interpreter','latex')
xlim([5,50])
legende=legend([p1,p2,p3],'Aggregate','Periphery','Core','Location','East','interpreter','latex');
set(legende,'FontSize',FontSize,'Interpreter','latex','color','none');
set(gca,'FontSize',round(FontSize*4/5));

set(h1,'PaperPositionMode','auto');
set(h1,'Position',[100 100 600 400]);

ylabels=get(gca,'YTickLabel'); % get the y axis labels
for i=1:size(ylabels,1)
    ylabels_modif{i}=sprintf('%.3g%%', str2double(ylabels{i})*100);
end
set(gca,'YTickLabel',ylabels_modif)
print(h1, '-depsc2','-loose',['pics/Figure_3a.eps'])


%% plot for different beta
Calibration
betas = 0:0.05:0.4;
delta_DL_per = [];
delta_DL_core = [];
delta_TDL = [];
for i=1:length(betas)
    beta = betas(i);

    % impact on peripheral entity
    delta_DL_per(i) = f(K-1,beta)/f(K,beta) + (1-pi^(2*N/3-1))/(1-pi) * xi(alpha_CCP)/xi(alpha_uc) * f(1,beta)/f(K,beta) - 1;

    % impact on core entity
    delta_DL_core(i) = f(K-1,beta)/f(K,beta) + pi^(2*N/3-1) * 6*G_per/((N-3)+6*G_per) * (1-pi^(N/3))/(1-pi) * xi(alpha_CCP)/xi(alpha_uc) * f(1,beta)/f(K,beta)-1;

    % impact on aggregate expected default loss
    eta_agg = 6*G_per/(12*G_per + N - 3);
    delta_TDL(i) = xi(alpha_CCP)/xi(alpha_uc)*f(1,beta)/f(K,beta)*eta_agg + f(K-1,beta)/f(K,beta)-1;
end


FontSize = 19;
linetypes = {'-','-',':','-.','--','--',':'};
linemarkers = {'none','o','none','.','s','d','+'};

h1 = figure();
hold on
p1=plot(betas,delta_TDL,'-s','Color','black','LineWidth',1.2);
p2=plot(betas,delta_DL_per,'--red','LineWidth',1.2);
p3=plot(betas,delta_DL_core,'-blue','LineWidth',1.2);
grid on
xlabel('Systematic risk exposure ($\beta$)','FontSize',10,'Interpreter','latex')
ylabel('$$\Delta$$Expected default loss ($$\Delta DL$$)','FontSize',10,'Interpreter','latex')
legende=legend([p1,p2,p3],'Aggregate','Periphery','Core','Location','West','interpreter','latex');
set(legende,'FontSize',FontSize,'Interpreter','latex','color','none');
set(gca,'FontSize',round(FontSize*4/5));

set(h1,'PaperPositionMode','auto');
set(h1,'Position',[100 100 600 400]);

ylabels=get(gca,'YTickLabel'); % get the y axis labels
for i=1:size(ylabels,1)
    ylabels_modif{i}=sprintf('%.3g%%', str2double(ylabels{i})*100);
end
set(gca,'YTickLabel',ylabels_modif)
print(h1, '-depsc2','-loose',['pics/Figure_3b.eps'])
